package com.example.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.DataOutputStream;
import java.io.IOException;

public class DBOpenHelper extends SQLiteOpenHelper {
    private static final String DBNAME = "TTYCRecords.db";
    private static final int VERSION = 1;

    //private Class<?>[] modelClasses;
    public DBOpenHelper(Context context) {

        super(context, DBNAME, null, VERSION);
        //this.modelClasses=modelClasses;
        // TODO Auto-generated constructor stub
    }

    //创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
//创建数据库表
        Process process = null;
        DataOutputStream oStream = null;
        String database = "/data/data/com.example.qc/databases";
        String cmddata = "chmod 777 " + database + "/TTYCRecords.db" + "\n";
        try {
            process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        oStream = new DataOutputStream(process.getOutputStream());
        try {
            oStream.writeBytes(cmddata);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oStream.writeBytes("exit\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oStream.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        createTable(db);
    }


    private void createTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        StringBuilder sb1 = new StringBuilder();
        sb1.append("CREATE TABLE CardInfo(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL,FileName TEXT ,ModificationDate TEXT ,KEYCardCode TEXT ,DrawingNumber TEXT ,ProductName TEXT ,ProductModel TEXT ,ProductBatchNumber TEXT ,QcCardId TEXT )");
        db.execSQL(sb1.toString());

        StringBuilder sb2 = new StringBuilder();
        sb2.append("CREATE TABLE ExamineItem(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,CapabilityTarget TEXT ,DataType TEXT ,ExamineMethod TEXT ,Produce TEXT ,Category TEXT ,Item TEXT ,CardCode TEXT ,Remark TEXT ,SpecialitySort TEXT ,State TEXT ,Attribute1 TEXT ,Attribute2 TEXT ,Attribute3 TEXT ,Attribute4 TEXT ,Attribute5 TEXT ,QcCardCode TEXT ,SequenceNumber TEXT ,TechnicPledge TEXT ,SpecialityNeed TEXT ,StepId TEXT ,MakeRes TEXT ,GradeType1 TEXT ,GradeType2 TEXT ,IsDemand TEXT ,ExamineDemandId TEXT ,WatchValue TEXT ,Conclusion TEXT ,Operator TEXT ,ReInterChecker TEXT ,Checker TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb2.toString());

        StringBuilder sb3 = new StringBuilder();
        sb3.append("CREATE TABLE FormFieldList(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, KEYCardCode TEXT )");
        db.execSQL(sb3.toString());


        StringBuilder sb4 = new StringBuilder();
        sb4.append("CREATE TABLE Item(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, Date TEXT ,OptionType TEXT ,Person TEXT ,Content TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb4.toString());


        StringBuilder sb5 = new StringBuilder();
        sb5.append("CREATE TABLE MostPortItem(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,CardCode TEXT ,DrawingNumber TEXT ,ProductName TEXT ,Remark TEXT ,ParaCode TEXT ,State TEXT ,SequenceNumber TEXT ,StepId TEXT ,ProductNumber TEXT ,Operator TEXT ,ReInnerChecker TEXT ,Checker TEXT ,CreateBy TEXT ,CreateDate TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb5.toString());

        StringBuilder sb6 = new StringBuilder();
        sb6.append("CREATE TABLE MultimediaDemand(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,QcCardId TEXT ,StepId TEXT ,ProductPosition TEXT ,RecordContent TEXT ,MultimediaType TEXT ,SequenceNumber TEXT ,State TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb6.toString());


        StringBuilder sb7 = new StringBuilder();
        sb7.append("CREATE TABLE MultimediaRecord(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,RecordingProductId TEXT ,MultimediaDemandId TEXT ,Recorder TEXT ,RecordDate TEXT ,State TEXT ,QcCardId TEXT ,StepId TEXT ,MultimediaNumber TEXT ,Verifier TEXT ,Technic TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb7.toString());


        StringBuilder sb8 = new StringBuilder();
        sb8.append("CREATE TABLE QcCard(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,Channel TEXT ,CreateBy TEXT ,CreateDate TEXT ,DrawingNumber TEXT ,LastModifyDate TEXT ,PlanAmount TEXT ,ProcessKind TEXT ,ProduceAmount TEXT ,ProductModel TEXT ,ProductName TEXT ,ProductNumber TEXT ,CardCode TEXT ,State TEXT ,TaskNumber TEXT ,TechnicNubmer TEXT ,TemplateNumber TEXT ,Workshop TEXT ,ProcessType TEXT ,CardType TEXT ,OutputTable TEXT ,NeedForm TEXT ,MainWorkshop TEXT ,Step TEXT ,MaterialInfo TEXT ,ProductBatchNumber TEXT ,NonmetalDate TEXT ,TechnicVerify TEXT ,AttemperVerify TEXT ,LastTechnicInfo TEXT ,LastAttemperInfo TEXT ,TaskRemark TEXT ,TaskItemId TEXT ,KEYCardCode TEXT ,TechMonitor TEXT ,ShopDirector TEXT ,Ration TEXT ,Attemper TEXT ,ManHour TEXT ,Team TEXT )");
        db.execSQL(sb8.toString());


        StringBuilder sb9 = new StringBuilder();
        sb9.append("CREATE TABLE RecordingProductList(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, KEYCardCode TEXT )");
        db.execSQL(sb9.toString());


        StringBuilder sb10 = new StringBuilder();
        sb10.append("CREATE TABLE User(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, type TEXT ,card_id TEXT ,username TEXT ,password TEXT ,realname TEXT )");
        db.execSQL(sb10.toString());

        StringBuilder sb11 = new StringBuilder();
        sb11.append("CREATE TABLE WorkingProcedure(id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL, SystemId TEXT ,Reject TEXT ,Character TEXT ,Content TEXT ,Produce TEXT ,Category TEXT ,CertificateAmount TEXT ,State TEXT ,Type TEXT ,Step TEXT ,Operator TEXT ,InterChecker TEXT ,Checker TEXT ,Technic TEXT ,MilitaryDelegate TEXT ,Designer TEXT ,OtherCooperator TEXT ,MiliDeleHandWriting TEXT ,DesignerHandWriting TEXT ,OtherCoopHandWriting TEXT ,SequenceNumber TEXT ,QcCardId TEXT ,PpsLink TEXT ,NeedForm TEXT ,KEYCardCode TEXT )");
        db.execSQL(sb11.toString());


    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

}
